package org.panda;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.panda.tcmoc.domain.SpProduct;
import org.panda.tcmoc.service.SpProductService;
import org.panda.tcmoc.service.impl.SpProductServiceImpl;
import org.panda.tcmoc.view.table.SpInquiryTableLabelProvider;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class SpRepairQueueDialog extends Dialog {

	public SpRepairQueueDialog(Shell parentShell) {
		super(parentShell);
	}

	private Table table;
	private SpProductService service = new SpProductServiceImpl();
	private SpProduct product;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();

		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setText("Repair Quue ");

		TableViewer tableViewer = new TableViewer(container, SWT.BORDER
				| SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				buttonPressed(IDialogConstants.OK_ID);
			}
		});
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TableColumn tblclmnBeginDate = new TableColumn(table, SWT.NONE);
		tblclmnBeginDate.setWidth(100);
		tblclmnBeginDate.setText("Begin Date");

		TableViewerColumn tableViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnDealTicketNo = tableViewerColumn.getColumn();
		tblclmnDealTicketNo.setWidth(200);
		tblclmnDealTicketNo.setText("Deal Ticket No");

		table.setHeaderVisible(true);

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn tblclmnCnyAmount = tableViewerColumn_1.getColumn();
		tblclmnCnyAmount.setWidth(200);
		tblclmnCnyAmount.setText("CNY Amount");
		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
		tableViewer.setLabelProvider(new SpInquiryTableLabelProvider());

		tableViewer.setInput(service.findUnrepairTransactions());

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */

	public SpProduct getRepairProduct() {
		return this.product;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			this.product = (SpProduct) table.getSelection()[0].getData();
		}

		super.buttonPressed(buttonId);
	}

}
